import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_controller_lifecycle/2%E3%80%81GetxController%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/life_cycle_page.dart';
import 'package:getx_controller_lifecycle/3%E3%80%81GetxController%E4%B8%8D%E9%9A%8F%E8%B7%AF%E7%94%B1%E9%80%80%E5%87%BA%E8%80%8C%E9%94%80%E6%AF%81/permanent_page.dart';
import '1、是否必须先调用Get.put/no_put_page.dart';
import '4、Get.put和Get.to同一个类不同实例/put_same_class_page.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key, required this.title});

  final String title;

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  static List<PageAction> source = [
    PageAction(name: "Get.put是否必须先调用？", function: () => toNoPutPage(source[0])),
    PageAction(name: "GexController生命周期", function: () => toLifeCyclePage(source[1])),
    PageAction(name: "GexController不随路由退出而销毁", function: () => toPermanentPage(source[2])),
    PageAction(name: "Get.put和Get.to同一个类不同实例", function: () => toPutSameClassPage(source[3])),
  ];

  static void toNoPutPage(PageAction action) {
    Get.to(()=> NoPutPage(),arguments: action);
  }

  static void toLifeCyclePage(PageAction action) {
    Get.to(() => const LifeCyclePage(), arguments: action);
  }

  static void toPermanentPage(PageAction action) {
    Get.to(() => PermanetPage(), arguments: action);
  }

  static void toPutSameClassPage(PageAction action) {
    Get.to(() => PutSameClassPage(), arguments: action);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      backgroundColor: Colors.white,
      body: ListView.builder(
        itemCount: source.length,
        itemExtent: 50,
        itemBuilder: (context,index) {
          PageAction model = source[index];
          return HomePageListRow(model: model,);
        }
      )
    );
  }
}

class HomePageListRow extends StatelessWidget {
  const HomePageListRow({super.key, required this.model});
  final PageAction model;

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () => model.function(),
      child: Column(
        children: [
          Expanded(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                const SizedBox(width: 10,),
                Expanded(
                  child: Text(
                    model.name,
                    style: const TextStyle(
                        color: Colors.black,
                        fontSize: 15
                    ),
                  ),
                ),
                const SizedBox(width: 10,),
                const Icon(
                  Icons.keyboard_arrow_right_rounded,
                  size: 20,
                ),
                const SizedBox(width: 5,),
              ],
            ),
          ),
          const Padding(
            padding: EdgeInsets.symmetric(horizontal: 10),
            child: SizedBox(
              width: double.infinity,
              height: 0.5,
              child: DecoratedBox(
                decoration: BoxDecoration(
                    color: Colors.grey
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}

class PageAction {
  PageAction({required this.name, required this.function});
  final String name;
  final Function function;
}